﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using Pavilion.DesignerTool.ViewTools.ConnectableListBox;
using Pavilion.DesignerTool.ViewTools;

namespace Pavilion.DesignerTool
{
    public class LinkWrapper : IConnectonator
    {
        public event Action<LinkWrapper> Disposed;

        public Link Link { get; private set; }
        public OpenCriteria OpenCriteria { get { return Link.OpenCriteria; } set { Link.OpenCriteria = value; LinkTransfer.Update(this); } }
        public ContainerNodeWrapper ContainerWrapperParent { get; private set; }
        public IConnectableItemContainer ParentContainer { get { return ContainerWrapperParent; } }
        public ObservableCollection<LockWrapper> LockWrapperCollection { get; private set; }
        public IConnectableItem Start { get; set; }
        public IConnectableItem End { get; set; }

        public DragAwareListBoxItem StartLBI { get; set; }
        public DragAwareListBoxItem EndLBI { get; set; }

        public LinkWrapper(Link link, IConnectableItem start, IConnectableItem end, ContainerNodeWrapper parent)
        {           
            this.Link = link;
            this.Start = start;
            this.End = end;
            this.ContainerWrapperParent = parent;
            LockWrapperCollection = new ObservableCollection<LockWrapper>();

            foreach (var Lock in this.Link.LockCollection)
            {
                LockWrapper lockWrapper = new LockWrapper(Lock, this);
                LockWrapperCollection.Add(lockWrapper);
            }

            Link.Disposed += (sender, e) => Dispose();
        }

        public void Dispose()
        {
            LockWrapperCollection.Clear();
            OnDisposed();
            Disposed = null;
        }

        internal void AddLockWrapper(LockWrapper lockWrapper)
        {
            LockWrapperCollection.Add(lockWrapper);
            Link.AddLock(lockWrapper.Lock);
            LockTransfer.Add(lockWrapper);

            foreach (PropertyItemBase propertyItem in lockWrapper.PropertyCategory.Properties)
                LockTransfer.Update(propertyItem as LockPropertyItem);
        }

        internal void RemoveLockWrapper(LockWrapper lockWrapper)
        {
            int lockIndex = Link.IndexOfLock(lockWrapper.Lock);
            this.LockWrapperCollection.Remove(lockWrapper);
            Link.RemoveLock(lockWrapper.Lock);

            LockTransfer.Remove(lockWrapper, lockIndex);
        }

        internal void RemoveLock(ILock lockItem)
        {
            foreach (LockWrapper lockWrapper in LockWrapperCollection)
            {
                if (lockWrapper.Lock == lockItem)
                {
                    RemoveLockWrapper(lockWrapper);
                    return;
                }
            }
        }

        private void OnDisposed()
        {
            if (Disposed != null)
                Disposed(this);
        }
    }
}
